﻿using B.MESUI.Login.EquipmentManagement.DTO;
using B.MESUI.Login.EquipmentManagement.Model;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Standard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace B.MESUI.Login.EquipmentManagement
{
    public partial class DeviceTypeDialog : DevExpress.XtraEditors.XtraForm
    {
        private string id;

        public DeviceTypeDialog(string id)
        {
            InitializeComponent();
            this.CenterToParent();
            this.Size = new Size(1100, 600);
            this.id = id;
        }
        
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static class DeviceData
        {
            // 你可以添加任意全局变量
            public static B.MESUI.Login.EquipmentManagement.Model.DeviceType Model {  get; set; }
            
        }


        private async void DeviceTypeDialog_Load(object sender, EventArgs e)
        {
            using (var client = new HttpClient())
            {
                // 替换为你的API实际地址
                string url = $"{BaseUrl.Url}DeviceType/Get?id={id}";
                var response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                var json = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<ApiResult<B.MESUI.Login.EquipmentManagement.Model.DeviceType>>(json);
                DeviceData.Model=result.ResultData;
                textEdit1.Text= DeviceData.Model.Code;
                textEdit2.Text=DeviceData.Model.Name;
                textEdit3.Text=DeviceData.Model.Description;
            }
            }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            // 非空校验
            if (string.IsNullOrWhiteSpace(textEdit1.Text))
            {
                MessageBox.Show("名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textEdit1.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(textEdit2.Text))
            {
                MessageBox.Show("编码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textEdit2.Focus();
                return;
            }
            DeviceData.Model.Code=textEdit1.Text;
           DeviceData.Model.Name=textEdit2.Text;
           DeviceData.Model.Description=textEdit3.Text;
            using (var client = new HttpClient())
            { 
                string url = $"{BaseUrl.Url}DeviceType/Update";
                var jsonStr = JsonConvert.SerializeObject(DeviceData.Model);
                var content = new StringContent(jsonStr, Encoding.UTF8,"application/json");
                var response = await client.PutAsync(url,content);
                response.EnsureSuccessStatusCode();
                var json = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<ApiResult<int>>(json);
                MessageBox.Show(result.Message);
                // 保存逻辑...
                this.DialogResult = DialogResult.OK; // 关闭窗口并返回OK
                this.Close();
            }
        }
    }
}